package com.bdqn.t320.ch12.controller;


import com.bdqn.t320.ch12.entity.User;
import com.bdqn.t320.ch12.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class ExampleController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model) {
        User user = userService.getUser(usrId);
        model.addAttribute("user", user);
        return "demo/hello";
    }

    @GetMapping(value = "/string")
    public String getString(Model model, HttpServletRequest request) {//演示赋值、字符串拼接
        model.addAttribute("userName", "ktjiaoyu");
        request.setAttribute("test", "request");
        request.getSession().setAttribute("test", "session");
        model.addAttribute("date", new Date());
        return "demo/string";
    }

    @GetMapping(value = "/th")
    public String th(Model model, HttpServletRequest request) {//演示赋值、字符串拼接
        model.addAttribute("userName", "bdqn");
        return "demo/th";
    }

    @GetMapping(value = "/index")
    public String index() {
        return "demo/index";
    }

    @GetMapping(value = "/fragment")
    public String layout() {
        return "demo/fragment";
    }

    @GetMapping(value = "/test")
    public String test() {
        return "demo/test";
    }
}
